package com.java.cacheavalanche.controller;


import com.java.cacheavalanche.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class QueryController {

    @Autowired
    private QueryService queryService;


    /**
     * 查询做缓存 version1
     * 未处理缓存雪崩问题
     * @return
     */
    @GetMapping("query_v1")
    public List<String> queryV1(){
       return queryService.queryV1();
    }


    /**
     * 查询做缓存 version2
     * 处理缓存雪崩问题
     * @return
     */
    @GetMapping("query_v2")
    public List<String> queryV2(){
        return queryService.queryV2();
    }


    public void  luaHashSet(){

    }


    /**
     * 更新库存
     */
    @GetMapping("/updateInventory")
    public Map<String,Object>  updateInventory(){
        queryService.updateInventory(1);
        return queryService.getInventory();
    }

    /**
     * 更新库存
     */
    @GetMapping("/updateInventoryV2")
    public Map<String,Object>  updateInventoryV2(){
        queryService.updateInventoryV2(1);
        return queryService.getInventory();
    }
    /**
     * 获取库存值
     * @return
     */
    @GetMapping("/getInventory")
    public Map<String,Object> getInventory(){
        return queryService.getInventory();
    }


    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/testMap")
    public Object testMap(String accountNo){
        Object o = redisTemplate.opsForValue().get("1dhdh" + accountNo);
        if (null == o){
            //设置值
            redisTemplate.opsForValue().set("1dhdh" + accountNo, accountNo);
            redisTemplate.expire("1dhdh" + accountNo, 86400, TimeUnit.SECONDS);
        }
        return o;
    }

}
